#include "Draw.h"

bool InitBMPs(dxManager* dxMgr, IDirect3DSurface9* cardImages[], IDirect3DSurface9* colorImages[])
{
	cardImages[BLACK] = dxMgr->getSurfaceFromBitmap("./BMP/CardImageBlack.bmp");
	cardImages[SILVER] = dxMgr->getSurfaceFromBitmap("./BMP/CardImageSilver.bmp");
	cardImages[BLUE] = dxMgr->getSurfaceFromBitmap("./BMP/CardImageBlue.bmp");
	cardImages[YELLOW] = dxMgr->getSurfaceFromBitmap("./BMP/CardImageYellow.bmp");
	cardImages[GREEN] = dxMgr->getSurfaceFromBitmap("./BMP/CardImageGreen.bmp");
	cardImages[ORANGE] = dxMgr->getSurfaceFromBitmap("./BMP/CardImageOrange.bmp");
	cardImages[PURPLE] = dxMgr->getSurfaceFromBitmap("./BMP/CardImagePurple.bmp");
	cardImages[RED] = dxMgr->getSurfaceFromBitmap("./BMP/CardImageRed.bmp");

	colorImages[BLUE - 2] = dxMgr->getSurfaceFromBitmap("./BMP/Blue.bmp");
	colorImages[YELLOW - 2] = dxMgr->getSurfaceFromBitmap("./BMP/Yellow.bmp");
	colorImages[GREEN - 2] = dxMgr->getSurfaceFromBitmap("./BMP/Green.bmp");
	colorImages[ORANGE - 2] = dxMgr->getSurfaceFromBitmap("./BMP/Orange.bmp");
	colorImages[PURPLE - 2] = dxMgr->getSurfaceFromBitmap("./BMP/Purple.bmp");
	colorImages[RED - 2] = dxMgr->getSurfaceFromBitmap("./BMP/Red.bmp");

	for(int i=0; i < 8; ++i)
	{
		if(cardImages[i] == NULL)
			return false;
		if(colorImages[i] == NULL && i < 6)
			return false;
	}

	return true;
}

void DrawCards(dxManager* dxMgr, Player& activePlayer, Player& inactivePlayer,
				const int& cardWidth, const int& cardHeight,
				const int* cardX, const int* cardY,
				const int& phase)
{
	static const int bottomCardBoardX[7] = { 162, 257, 354, 452, 209, 306, 400 };
	static const int bottomCardBoardY[2] = { 310, 441 };
	static const int topCardBoardX[7] = { 160, 256, 353, 451, 208, 305, 399 };
	static const int topCardBoardY[2] = { 166, 33 };

	RECT src, dest;
	src.top = dest.top = 0;
	src.left = dest.left = 0;
	src.right= dest.right = cardWidth;
	src.bottom = dest.bottom = cardHeight;

	//draw card onto hand
	for(int i=0; i < 6; ++i)
	{
		//check if that hand has a card or not
		if( activePlayer.HasHandCard(i) )
		{
			dest.top = cardY[i/2];
			dest.left = cardX[i%2];
			dest.right = dest.left + cardWidth - 3;
			dest.bottom = dest.top + cardHeight - 5;
			dxMgr->blitToSurface( activePlayer.ShowHandCard(i)->GetCardImage(), &src, &dest);
		}
	}
	//draw card onto field
	for(int i=0; i < 7; ++i)
	{
		//check if that field has a card or not
		if( activePlayer.HasFieldCard(i) )
		{
			i < 4 ?  dest.top = bottomCardBoardY[0] : dest.top = bottomCardBoardY[1];
			dest.left = bottomCardBoardX[i];
			dest.right = dest.left + cardWidth;
			dest.bottom = dest.top + cardHeight;
			dxMgr->blitToSurface( activePlayer.ShowFieldCard(i)->GetCardImage(), &src, &dest);
		}
	}

	//test how many cards to show player
	int endCard = 0;
	if(phase >= 5 && phase <= 11)
		endCard = 4;
	else if(phase == 13 || phase == 15)
		endCard = 6;
	else if(phase == 16)
		endCard = 7;

	//draw opponet cards on top
	for(int i=0; i < endCard; ++i)
	{
		//check if that field has a card or not
		if( inactivePlayer.HasFieldCard(i) )
		{
			i < 4 ?  dest.top = topCardBoardY[0] : dest.top = topCardBoardY[1];
			dest.left = topCardBoardX[i];
			dest.right = dest.left + cardWidth;
			dest.bottom = dest.top + cardHeight;
			//testing code
			dxMgr->blitToSurface( inactivePlayer.ShowFieldCard(i)->GetCardImage(), &src, &dest);
		}
	}//end for
}


void DrawColor(dxManager* dxMgr, IDirect3DSurface9* crdImages[8],
			   const int& cardWidth, const int& cardHeight,
			   const int* cardX, const int* cardY)
{
	RECT src, dest;
	src.top = dest.top = 0;
	src.left = dest.left = 0;
	src.right= dest.right = cardWidth;
	src.bottom = dest.bottom = cardHeight;

	for(int i=0; i < 6; ++i)
	{
		dest.top = cardY[i/2];
		dest.left = cardX[i%2];
		dest.right = dest.left + cardWidth - 3;
		dest.bottom = dest.top + cardHeight - 3;
		dxMgr->blitToSurface(crdImages[i+2], &src, &dest);
	}
}

void DrawPreview(dxManager* dxMgr, Player& player, const int& cursorMode,
				 const int& cardWidth, const int& cardHeight,
				 const int& handNum, const int& fieldNum)
{
	static const int previewWidth = 202, previewHeight = 224;
	static const int previewX = 555, previewY = 1;

	RECT src, dest;
	src.top = dest.top = 0;
	src.left = dest.left = 0;
	src.right= dest.right = cardWidth;
	src.bottom = dest.bottom = cardHeight;

	SetRect(&dest, previewX, previewY, previewX + previewWidth, previewY + previewHeight);
	
	if(cursorMode)
	{
		if(player.HasFieldCard(fieldNum))
			dxMgr->blitToSurface( player.ShowFieldCard(fieldNum)->GetCardImage(), &src, &dest);
	}
	else
	{
		if(player.HasHandCard(handNum))
			dxMgr->blitToSurface( player.ShowHandCard(handNum)->GetCardImage(), &src, &dest);
	}
}

void DrawCardOverlay(dxManager* dxMgr, LPDIRECT3DTEXTURE9 textureImage[2],
					 Player& p1, Player& p2, const int& p1Color, const int& p2Color)
{
	static const int bottomCardBoardX[7] = { 162, 257, 354, 452, 209, 306, 400 };
	static const int bottomCardBoardY[2] = { 310, 441 };
	static const int topCardBoardX[7] = { 160, 256, 353, 451, 208, 305, 399 };
	static const int topCardBoardY[2] = { 166, 33 };
	D3DXVECTOR3 dest(0, 0, 0);

	//set the texture of both playe color
	if(textureImage[0] == NULL)
	{
		switch(p1Color)
		{
		case BLUE:
			dxMgr->GetTextureFromFile("./BMP/CardImageBlue.bmp", textureImage);
			break;
		case YELLOW:
			dxMgr->GetTextureFromFile("./BMP/CardImageYellow.bmp", textureImage);
			break;
		case GREEN:
			dxMgr->GetTextureFromFile("./BMP/CardImageGreen.bmp", textureImage);
			break;
		case ORANGE:
			dxMgr->GetTextureFromFile("./BMP/CardImageOrange.bmp", textureImage);
			break;
		case PURPLE:
			dxMgr->GetTextureFromFile("./BMP/CardImagePurple.bmp", textureImage);
			break;
		case RED:
			dxMgr->GetTextureFromFile("./BMP/CardImageRed.bmp", textureImage);
			break;
		}
		switch(p2Color)
		{
		case BLUE:
			dxMgr->GetTextureFromFile("./BMP/CardImageBlue.bmp", textureImage+1);
			break;
		case YELLOW:
			dxMgr->GetTextureFromFile("./BMP/CardImageYellow.bmp", textureImage+1);
			break;
		case GREEN:
			dxMgr->GetTextureFromFile("./BMP/CardImageGreen.bmp", textureImage+1);
			break;
		case ORANGE:
			dxMgr->GetTextureFromFile("./BMP/CardImageOrange.bmp", textureImage+1);
			break;
		case PURPLE:
			dxMgr->GetTextureFromFile("./BMP/CardImagePurple.bmp", textureImage+1);
			break;
		case RED:
			dxMgr->GetTextureFromFile("./BMP/CardImageRed.bmp", textureImage+1);
			break;
		}
	}//end if

	//change player 1 silver cards
	for(int i=0; i < 7; ++i)
	{
		if(p1.ShowFieldCard(i)->getColor() == 0x40)
		{
			dest.x = bottomCardBoardX[i];
			i > 3 ? dest.y = bottomCardBoardY[1] : dest.y = bottomCardBoardY[0];
			dxMgr->ShowTextureOverlay(textureImage[0], dest);
		}
	}

	//change player 2 silver cards
	for(int i=0; i < 7; ++i)
	{
		if(p2.ShowFieldCard(i)->getColor() == 0x40)
		{
			dest.x = topCardBoardX[i];
			i > 3 ? dest.y = topCardBoardY[1] : dest.y = topCardBoardY[0];
			dxMgr->ShowTextureOverlay(textureImage[1], dest);
		}
	}
}

void CardCheckForAmount(Player& player, const int& phase)
{
	int endCheck = 0;
	//1 turn check 4 cards, 2 turn check 7 cards
	phase < 9 ? endCheck = 4 : endCheck = 7;

	for(int i=0; i < endCheck; ++i) //goes through field for empty space
	{
		if(!player.HasFieldCard(i))
		{
			for(int j=0; ; ++j) //goes throught hand for first avaliable card
				if(player.HasHandCard(j))
				{
					player.SwapHandAndField(j, i);
					break;
				}
		}//end field if
	}//end field for
}

void InitColorVictory(Player& p1, Player& p2, int& color1, int& color2)
{
	int temp1 = p1.ShowLastCardInDeck()->getColor();
	int temp2 = p2.ShowLastCardInDeck()->getColor();

	//get the color, -2 becuase black = 0, silver = 1, and they are not valid
	switch(temp1)
	{
	case 0x1:
		color1 = RED - 2;
		break;
	case 0x2:
		color1 = YELLOW - 2;
		break;
	case 0x4:
		color1 = BLUE - 2;
		break;
	case 0x8:
		color1 = GREEN - 2;
		break;
	case 0x10:
		color1 = PURPLE - 2;
		break;
	case 0x80:
		color1 = ORANGE - 2;
		break;
	}

	switch(temp2)
	{
	case 0x1:
		color2 = RED - 2;
		break;
	case 0x2:
		color2 = YELLOW - 2;
		break;
	case 0x4:
		color2 = BLUE - 2;
		break;
	case 0x8:
		color2 = GREEN - 2;
		break;
	case 0x10:
		color2 = PURPLE - 2;
		break;
	case 0x80:
		color2 = ORANGE - 2;
		break;
	}

	if(color1 == color2)
		color2 = -1;
}

void CalculateVictoryColorPoint(Player& p1, Player& p2, int& p1Color1, int& p1Color2,
								int& p2Color1, int& p2Color2)
{
	static int color1 = p1.ShowLastCardInDeck()->getColor();
	static int color2 = p2.ShowLastCardInDeck()->getColor();

	int p1Temp1 = 0, p1Temp2 = 0;
	int p2Temp1 = 0, p2Temp2 = 0;

	//only check the right color
	if(color1 == 0x20 || color1 == 0x40)
		color1 = 0;
	if(color2 == 0x20 || color2 == 0x40)
		color2 = 0;
	if(color1 == color2)
		color2 = 0;

	//get player color cards
	for(int i=0; i < 7; ++i)
	{
		//get p1 color cards
		if(p1.ShowFieldCard(i) != NULL)
		{
			if(p1.ShowFieldCard(i)->getColor() == color1)
				p1Temp1++;
			else if(p1.ShowFieldCard(i)->getColor() == color2)
				p1Temp2++;
		}

		//get p2 color cards
		if(p2.ShowFieldCard(i) != NULL)
		{
			if(p2.ShowFieldCard(i)->getColor() == color1)
				p2Temp1++;
			else if(p2.ShowFieldCard(i)->getColor() == color2)
				p2Temp2++;
		}
	}//end for

	p1Color1 = p1Temp1;
	p1Color2 = p1Temp2;
	p2Color1 = p2Temp1;
	p2Color2 = p2Temp2;
}

void PrintVictoryColor(dxManager* dxMgr, IDirect3DSurface9* colorImages[],
					   const int& p1Color1, const int& p1Color2,
					   const int& p2Color1, const int& p2Color2,
					   const int& color1, const int& color2)
{
	const int X[3] = { 35, 67, 110 };
	const int Y[2] = { 60, 505 };
	const int inSize = 5;
	bool twoColor = false;;

	RECT dest;

	//check how many color is there
	if(color1 == -1 && color2 == -1)
		return;
	else if(color1 != -1 && color2 != -1)
		twoColor = true;

	if(twoColor)
	{
		//show the color
		SetRect(&dest, X[0], Y[0], X[0]+inSize, Y[0]+inSize);
		dxMgr->blitToSurface(colorImages[color1], NULL, &dest);
		dest.top = Y[1];
		dest.bottom = Y[1]+inSize;
		dxMgr->blitToSurface(colorImages[color1], NULL, &dest);
		dest.left = X[2];
		dest.right = X[2]+inSize;
		dxMgr->blitToSurface(colorImages[color2], NULL, &dest);
		dest.top = Y[0];
		dest.bottom = Y[0]+inSize;
		dxMgr->blitToSurface(colorImages[color2], NULL, &dest);
		dxMgr->printColorVictoryText(p2Color1, p2Color2, p1Color1, p1Color2);
	}
	else
	{
		SetRect(&dest, X[1], Y[0], X[1]+inSize, Y[0]+inSize);

		if(color1 > 0)
			dxMgr->blitToSurface(colorImages[color1], NULL, &dest);
		else
			dxMgr->blitToSurface(colorImages[color2], NULL, &dest);

		dest.top = Y[1];
		dest.bottom = Y[1]+inSize;

		if(color1 > 0)
			dxMgr->blitToSurface(colorImages[color1], NULL, &dest);
		else
			dxMgr->blitToSurface(colorImages[color2], NULL, &dest);

		dxMgr->printColorVictoryText(p2Color1, p2Color2, p1Color1, p1Color2);
	}
}